package com.bop.dehong.huodong;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.base.dehong.http.RequestCallBack;
import com.base.dehong.util.ToastUtils;
import com.bop.dehong.R;
import com.bop.dehong.base.BaseActivity;
import com.bop.dehong.base.DeHongApp;
import com.bop.dehong.base.TopManager;
import com.bop.dehong.config.Config;
import com.bop.dehong.decaifu.ToubiaoActivity;
import com.bop.dehong.http.model.GetHuodongModel;
import com.bop.dehong.info.GetHuodongInfo;
import com.bop.dehong.info.HuodongInfo;
import com.bop.dehong.login.LoginActivity;
import com.bop.dehong.webview.WebViewActivity;
import com.bop.dehong.widget.PullToRefreshView;
import com.bop.dehong.widget.PullToRefreshView.OnFooterRefreshListener;
import com.bop.dehong.widget.PullToRefreshView.OnHeaderRefreshListener;

public class HuodongAct extends BaseActivity implements OnHeaderRefreshListener, OnFooterRefreshListener {
	private PullToRefreshView mRefreshView;
	private ListView mListView;
	private int REQ_REFRESH = 2;
	private int REQ_LOAD  = 1;
	private int REQ_TYPE = 0;
	private int mCurrentPage = 1;
	private int mTotalPage = 0;
	private HuodongListAdapter mAdapter;
	private GetHuodongModel mGetModel;
	
	@Override
	protected void initTop() {
		TAG = getClass().getName();
		View mView = getView(R.id.huodong_top);
		TopManager manager = new TopManager(mView);
		manager.setTitle(R.string.huodong_list);
		manager.setLeftImageVisibility(View.VISIBLE);
	}

	@Override
	protected void initUI() {
		mListView = getView(R.id.listView1);
		mRefreshView = getView(R.id.pullToRefreshView1);
		mRefreshView.setOnHeaderRefreshListener(this);
		mRefreshView.setOnFooterRefreshListener(this);
		mListView.setOnItemClickListener(this);
		if (mAdapter == null) {
			mAdapter =  new HuodongListAdapter(this);
		}
		mListView.setAdapter(mAdapter);
	}

	@Override
	protected void initData() {
		if (mGetModel == null) {
			mGetModel = new GetHuodongModel(this, callback);
		}
		String uid = DeHongApp.mLoginEntity.uid;
		if (TextUtils.isEmpty(uid))
		{
			ToastUtils.show(this, "你还未登录，请先登录！");
			Intent intent = new Intent(this, LoginActivity.class);
			intent.putExtra(Config.GO_BACK, true);
			startActivityForResult(intent, 100);
			return;
		}
		showProgressDialog();
		mGetModel.doNet(uid, "0", mCurrentPage + "");
	}

	RequestCallBack<GetHuodongInfo> callback = new RequestCallBack<GetHuodongInfo>() {

		@Override
		public void end() {
			disappearProgressDialog();
		}

		@Override
		public void onSuccess(GetHuodongInfo info) {
			super.onSuccess(info);
			mRefreshView.onRefreshComplete();
			if (REQ_TYPE == REQ_REFRESH) {
				mAdapter.clear();
			}
			if (info.list.size() > 0) {
				mAdapter.appendToList(info.list);
			}else {
				ToastUtils.show(HuodongAct.this, "没有数据可加载");
			}
			mTotalPage = info.totalpages;
		}
		
	};
	
	@Override
	protected int getContentViewId() {
		// TODO Auto-generated method stub
		return R.layout.activity_huodong;
	}

	@Override
	public void onFooterRefresh(PullToRefreshView view) {
		REQ_TYPE = REQ_LOAD;
		if (mCurrentPage < mTotalPage) {
			mCurrentPage ++;
			initData();
			mRefreshView.onFooterRefreshComplete();
		}else{
			ToastUtils.show(this, "没有数据可加载！");
			mRefreshView.onFooterRefreshComplete();
		}
	}

	@Override
	public void onHeaderRefresh(PullToRefreshView view) {
		REQ_TYPE = REQ_REFRESH;
		mCurrentPage = 1;
		initData();
		mRefreshView.onHeaderRefreshComplete();
	}


	@Override
	public void onClick(View v)
	{
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id)
	{
		HuodongInfo info = (HuodongInfo) parent.getAdapter().getItem(position);
		if (!TextUtils.isEmpty(info.bid)) {
			Bundle bundle = new Bundle();
			bundle.putString(Config.CAT_ID, info.bid);
			openActivity(ToubiaoActivity.class, bundle);
		}else {
			String title = info.title;
			String url = info.linkurl;
			if (!TextUtils.isEmpty(title) && !TextUtils.isEmpty(url)) {
				Intent intent = new Intent(this, WebViewActivity.class);
				intent.putExtra("title", title);
				intent.putExtra("url", url);
				intent.putExtra(Config.GO_BACK, true);
				startActivity(intent);
			}
		}
	}

	@Override
	protected void onActivityResult(int arg0, int resultCode, Intent arg2) {
		super.onActivityResult(arg0, resultCode, arg2);
		if (resultCode != RESULT_OK) {
			finishCurrentActivity();
			return;
		}
		initData();
	}
}
